﻿using System;
using System.Windows.Navigation;

namespace Songhay
{
    /// <summary>
    /// Represents the strategy of an asynchronous navigation operation.
    /// </summary>
    /// <remarks>
    /// You must override <c>BeginLoad</c> and <c>CanLoad</c>.
    ///
    /// For more, see “Opening up Silverlight 4 Navigation: Introduction to INavigationContentLoader”
    /// [http://www.davidpoll.com/2009/11/30/opening-up-silverlight-4-navigation-introduction-to-inavigationcontentloader/]
    /// </remarks>
    public abstract class NavigationContentLoader : INavigationContentLoader
    {
        #region INavigationContentLoader Members

        /// <summary>
        /// Begins asynchronous loading of the content for the specified target URI.
        /// </summary>
        /// <param name="targetUri">The URI to load content for.</param>
        /// <param name="currentUri">The URI that is currently loaded.</param>
        /// <param name="userCallback">The method to call when the content finishes loading.</param>
        /// <param name="asyncState">An object for storing custom state information.</param>
        /// <returns>
        /// An object that stores information about the asynchronous operation.
        /// </returns>
        public abstract IAsyncResult BeginLoad(Uri targetUri, Uri currentUri, AsyncCallback userCallback, object asyncState);

        /// <summary>
        /// Attempts to cancel content loading for the specified asynchronous operation.
        /// </summary>
        /// <param name="asyncResult">An object that identifies the asynchronous operation to cancel.</param>
        public virtual void CancelLoad(IAsyncResult asyncResult) { return; }

        /// <summary>
        /// Gets a value that indicates whether the specified URI can be loaded.
        /// </summary>
        /// <param name="targetUri">The URI to test.</param>
        /// <param name="currentUri">The URI that is currently loaded.</param>
        /// <returns>
        /// true if the URI can be loaded; otherwise, false.
        /// </returns>
        public abstract bool CanLoad(Uri targetUri, Uri currentUri);

        /// <summary>
        /// Completes the asynchronous content loading operation.
        /// </summary>
        /// <param name="asyncResult">An object that identifies the asynchronous operation.</param>
        /// <returns>
        /// An object that represents the result of the asynchronous content loading operation.
        /// </returns>
        public virtual LoadResult EndLoad(IAsyncResult asyncResult)
        {
            return new LoadResult(((NavigationContentLoaderAsyncResult)asyncResult).Result);
        }

        #endregion
    }
}
